<?php
include_once("../module/micro_shop/includes/plugin_coupons_class.php");
$couponsObj = new Coupons();

if($_GET['act'] && $_GET['act']=='forbid'){//禁用
    if($_GET['id']>0){
        $couponsObj->updateCouponsStatus($_GET['id'], '-1');
    }
    
    msg('?m=micro_shop&s=coupons_list.php');
}

if($_POST['act'] && $_POST['act']=='op'){//批量禁用
    $status = -1;
    if($_POST['submit']=='解禁'){
        $status = 0;
    }
    if(!empty($_POST['chk'])){
        $cpnsIds = implode(',', $_POST['chk']);
        $couponsObj->updateCouponsStatus($cpnsIds, $status);
    }
    
    msg('?m=micro_shop&s=coupons_list.php');
}

$str = "";
if($_GET['member_id']){
    $memIds = str_replace('，', ',', $_GET['member_id']);
    $str .= " and a.member_id in({$memIds})";
}
if($_GET['cpns_name']){
    $str .= " and a.title like '{$_GET['cpns_name']}%' ";
}
if(isset($_GET['status']) && $_GET['status']!=''){
    $str .= " and a.status='{$_GET['status']}'";
}
$sql = "select a.*,b.name as shop_name,c.member_id as use_member_id,c.ms_id,d.name as use_shop_name,e.mobile as use_mobile from ".MICROSHOPCOUPONS." AS a left join ".MICROSHOP." AS b on a.member_id=b.member_id left join ".ORDER." c on a.order_id=c.order_id left join ".MICROSHOP." d on c.ms_id=d.member_id left join ".MEMBER." e on c.member_id=e.userid where 1 {$str} order by a.start_time desc";
//=======分页=============
include_once("../includes/page_utf_class.php");
$page = new Page;
$page->listRows=20;
$db->query($sql);
$totalRows = $db->num_rows();
if (!$page->__get('totalRows')){
    $db->query($sql);
    $de['count'] = $page->totalRows = $db->num_rows();
}
$sql .= "  limit ".$page->firstRow.",".$page->listRows;
$de['count']=$de['count']?$de['count']:($_GET['totalRows']?$_GET['totalRows']:'0');
$de['page'] = $page->prompt();
$de['perPageRows'] = $page->listRows;
//=====================
$db->query($sql);
$de['list']=$db->getRows();

//查询礼包配置
$sql = "select id,name from ".MICROSHOPCOUPONSCONFIG." where status=1";
$db->query($sql);
$configArr = $db->getRows();

$tpl->assign('de',$de);
$tpl->assign('configArr',$configArr);
$tpl->display('coupons_list.htm');